#!/usr/bin/python
# coding=utf-8
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import os


class WebHookHandler(BaseHTTPRequestHandler):

    def do_POST(self):
        path = self.path
        exec_type = path[-3:-6:-1]
        print("URL--->[%s]  TYPE--->[%s]" % (path, exec_type))
        template_str = "Receive Success!"
        self.send_response(200)  # 设置响应状态码
        self.send_header("Content-type", "text/html")  # 设置响应头
        self.end_headers()
        self.wfile.write(template_str)  # 输出响应内容
        if "001" == exec_type:
            os.system("cd /opt/deploy-work/video-site && git pull")
        elif "002" == exec_type:
            os.system("sh /opt/script/deploy_website.sh")
        return


if __name__ == '__main__':
    try:
        http_server = HTTPServer(("", int(8080)), WebHookHandler)
        http_server.serve_forever()
    except KeyboardInterrupt:
        print("^C received, shutting down the web server")
        http_server.socket.close()

